package hust.att2.fragment;

import hust.att2.movieticketing.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SearchFilmFragment extends Fragment {

	private String mContent = "???";
	private static final String KEY_CONTENT = "TestFragment:Content";
	private Button btnSearch;
	private Spinner mCategorySpn;
	private EditText mFilmNameEd;
	private Spinner mDateSpn;
	private Spinner mTimeSpn;

	public static SearchFilmFragment newInstance(String content) {
		SearchFilmFragment fragment = new SearchFilmFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if ((savedInstanceState != null)
				&& savedInstanceState.containsKey(KEY_CONTENT)) {
			mContent = savedInstanceState.getString(KEY_CONTENT);
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(
				hust.att2.movieticketing.R.layout.search_fragment_layout, null);

		return root;
	}

	private String category;
	private String date;
	private String time;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mCategorySpn = (Spinner) getView().findViewById(R.id.category_spinner);
		ArrayAdapter<CharSequence> categoryAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.category,
						android.R.layout.simple_spinner_item);
		categoryAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mCategorySpn.setAdapter(categoryAdapter);
		mCategorySpn.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> spn, View arg1, int arg2,
					long arg3) {
				category = spn.getItemAtPosition(arg2).toString();
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		mDateSpn = (Spinner) getView().findViewById(R.id.date_spinner);
		ArrayAdapter<CharSequence> timeAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.date,
						android.R.layout.simple_spinner_item);
		timeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mDateSpn.setAdapter(timeAdapter);
		mDateSpn.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> spn, View arg1, int arg2,
					long arg3) {
				date = spn.getItemAtPosition(arg2).toString();
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		mTimeSpn = (Spinner) getView().findViewById(R.id.time_spinner);
		ArrayAdapter<CharSequence> dateAdapter = ArrayAdapter
				.createFromResource(getActivity(), R.array.time,
						android.R.layout.simple_spinner_item);
		dateAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mTimeSpn.setAdapter(dateAdapter);
		mTimeSpn.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> spn, View arg1, int arg2,
					long arg3) {
				time = spn.getItemAtPosition(arg2).toString();
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		mFilmNameEd = (EditText) getView().findViewById(R.id.editText1);
		btnSearch = (Button) this.getView().findViewById(R.id.button1);
		btnSearch.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Toast.makeText(
						getActivity(),
						"" + category + ";" + date + ";" + time + ";"
								+ mFilmNameEd.getText().toString(),
						Toast.LENGTH_SHORT).show();

			}
		});
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(KEY_CONTENT, mContent);
	}

}
